/**
 * 27/12/2013
 * 
 * World
 * 
 * Copyright GPL
 */
package com.jtmproject;

import java.io.Serializable;

/**
 * This class represents the world of the game.
 * 
 * @author Pello Xabier && Javier Tejedor
 */
public class World implements Serializable{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2235700670674489240L;
	
	private Ball ball;
	private Paddle paddleGamerA;
	private Paddle paddleGamerB;
	
	//to control the game over
	private boolean gameOver = false;
	
	/**
	 * constructor 
	 */
	public World(int goalsA, int goalsB){
		initialize(goalsA, goalsB);
	}
	
	/**
	 * this initializes the world
	 */
	private void initialize(int goalsA, int goalsB) {
		ball = new Ball();
		
		paddleGamerA = new Paddle(40, 0, goalsA);
		paddleGamerB = new Paddle(40, 145f, goalsB);

	}
	
	/**
	 * updates world status
	 * @param delta
	 */
	public void update (float delta) {
		//this updates the position of the ball. The position is marked by the vector. Only we have to add the values of the direction
		ball.getCurrentPosition().add(
				ball.getCurrentDirection().x * delta, ball.getCurrentDirection().y * delta, 0);

		//Has the paddleA touch the ball? YES
		if (ball.getCurrentPosition().x > paddleGamerA.getPosition().x && 
				ball.getCurrentPosition().x <= paddleGamerA.getPosition().x + 25 &&
				ball.getCurrentPosition().y < 5) {

			//Toggle the position
			ball.getCurrentDirection().y = ball.getSpeed();
			GameSounds.playSoundHitBall();
		} 
		
		//Has the paddleB touch the ball? YES
		if (ball.getCurrentPosition().x > paddleGamerB.getPosition().x && 
				ball.getCurrentPosition().x <= paddleGamerB.getPosition().x + 25 &&
				ball.getCurrentPosition().y > 140) {

			ball.getCurrentDirection().y = -ball.getSpeed();
			GameSounds.playSoundHitBall();
		} 
		
		//Has the paddleB touch the ball? NO
		if (ball.getCurrentPosition().y > 145 ) {
			GameSounds.playSoundLost();
			paddleGamerB.addGoal();
			setGameOver(true);

		}
		
		//Left wall is touched
		if (ball.getCurrentPosition().x < 0) {
			ball.getCurrentDirection().x = ball.getSpeed();
		}

		//Right wall is touched
		if (ball.getCurrentPosition().x > 95) {
			ball.getCurrentDirection().x = -ball.getSpeed();
		}
		
		//Has the paddleA touch the ball? NO
		if (ball.getCurrentPosition().y < 0) {
			GameSounds.playSoundLost();
			paddleGamerA.addGoal();
			setGameOver(true);
		}

	}

	/**
	 * returns the ball
	 * @return
	 */
	public Ball getBall() {
		return ball;
	}

	/**
	 * sets the ball
	 * @param ball
	 */
	public void setBall(Ball ball) {
		this.ball = ball;
	}

	/**
	 * returns the paddleGamerA
	 * @return
	 */
	public Paddle getPaddleGamerA() {
		return paddleGamerA;
	}
	
	/**
	 * returns the paddleGamerB
	 * @return 
	 */
	public Paddle getPaddleGamerB() {
		return paddleGamerB;
	}

	/**
	 * sets the paddle
	 * @param paddle
	 */
	public void setPala(Paddle paddle) {
		this.paddleGamerA = paddle;
	}

	/**
	 * checks if is game over activated
	 * @return
	 */
	public boolean isGameOver() {
		return gameOver;
	}
	
	/**
	 * set game over
	 * @param gameOver
	 */
	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}

	@Override
	public String toString() {
		return "World [ball=" + ball + ", paddleGamerA=" + paddleGamerA
				+ ", paddleGamerB=" + paddleGamerB + ", gameOver="
				+ gameOver + "]";
	}

}
